<!--
 * @Descripttion: 砍价活动详情
 * @Author: xiao li
 * @Date: 2021-01-19 09:27:46
 * @LastEditors: xiao li
 * @LastEditTime: 2021-01-26 17:47:38
-->

<template>
    <div class="lb-goods-bargain-detail">
        <top-nav :title="navTitle" :isBack="true"/>
        <div class="page-main"> <el-row class="page-search-form">
            <el-form
            :inline="true"
            :model="searchForm"
            ref="searchForm"
            >
              <el-form-item label="活动状态" prop="status">
              <el-select v-model="searchForm.status" placeholder="请选择活动状态">
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.id"
                    :label="item.title"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>

              <el-form-item label="">
                <lb-button size='medium' type='primary' opType='add' icon="el-icon-search" style="margin-right:5px" @click="getTableDataList(1)">{{$t('action.search')}}</lb-button>
                <lb-button size='medium' icon="el-icon-refresh-left" @click="resetForm">{{$t('action.reset')}}</lb-button>
              </el-form-item>
            </el-form>
          </el-row>
          <el-table
            v-loading='loading'
            :data="tableData"
          :header-cell-style="{background:'#f5f7fa',color:'#606266'}"
            tooltip-effect="dark"
            style="width: 100%"
           >
            <!-- <el-table-column
              prop="atv_title"
              label="活动名称">
            </el-table-column> -->
            <el-table-column
              prop="goods_name"
              label="砍价商品">
            </el-table-column>
            <el-table-column
              prop="spe_name"
              label="商品规格">
            </el-table-column>
            <el-table-column
              prop="nickName"
              label="发起人昵称">
            </el-table-column>
            <el-table-column
              prop="avatarUrl"
              label="头像">
                <template slot-scope="scope">
                  <lb-image :src="scope.row.avatarUrl" />
                </template>
            </el-table-column>
            <el-table-column
              prop="user_num"
              label="帮砍人数">
            </el-table-column>
            <el-table-column
              prop="have_num"
              label="剩余砍价人数">
                <template slot-scope="scope">
                  {{parseInt(scope.row.user_num - scope.row.have_num)}}
                </template>
            </el-table-column>
            <el-table-column
              prop="status"
              label="状态">
                <template slot-scope="scope">
                  {{statusType[scope.row.status]}}
                </template>
            </el-table-column>
            <el-table-column
              prop=""
              label="发起时间">
                <template slot-scope="scope">
                  <div>{{scope.row.create_time | handleTime(1)}}</div>
                  <div>{{scope.row.create_time | handleTime(2)}}</div>
                </template>
            </el-table-column>
          </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
        </div>
    </div>
</template>

<script>
export default {
  data () {
    return {
      navTitle: '',
      statusOptions: [{
        id: 0,
        title: '全部'
      }, {
        id: 1,
        title: '砍价中'
      }, {
        id: 2,
        title: '砍价成功'
      }, {
        id: -1,
        title: '砍价失败'
      }],
      statusType: {
        '-2': '砍价取消',
        '-1': '砍价超时',
        1: '砍价中',
        2: '砍价成功 未购买',
        3: '砍价成功 已购买',
        4: '订单支付失败',
        5: '退款成功'
      },
      loading: false,
      total: 0,
      searchForm: {
        page: 1,
        limit: 10,
        atv_id: 0,
        status: 0
      },
      tableData: [],
      multipleSelection: [],
      showTable: true
    }
  },
  created () {
    let {id} = this.$route.query
    this.searchForm.atv_id = id
    this.getTableDataList(id)
  },
  methods: {
    /**
     * @method: 重置表单
     */
    resetForm () {
      this.$refs['searchForm'].resetFields()
      this.getTableDataList(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
    },
    /**
     * @method 获取分类列表
     */
    getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.loading = true
      this.$api.bargainRecordList(this.searchForm).then(res => {
        this.loading = false
        if (res.code === 200) {
          this.tableData = res.data.data
          this.total = res.data.total
        }
      })
    },
    /**
     * @method 删除表格分类值
     */
    confirmDel (id) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.upperStatus(id, -1)
      }).catch(() => {

      })
    },
    /**
     * @method 上下架/删除分类
     */
    async upperStatus (id, status) {
      this.$api.restaurantCateUpdate({id, status}).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status !== -1) return
          this.searchForm.page = this.searchForm.page < Math.ceil((this.total - 1) / this.searchForm.limit) ? this.searchForm.page : Math.ceil((this.total - 1) / this.searchForm.limit)
          this.getTableDataList()
        } else {
          if (status === -1) return
          this.getTableDataList()
        }
      })
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
  .lb-goods-bargain-detail{
    width: 100%;
    .page-main{
      padding: 20px;
      .el-row{
        margin-bottom: 20px;
        .el-button{
          margin-right: 24px;
          &:last-child{
            margin-right: 0;
          }
        }
      }
    }
  }
</style>
